import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yp/config/index.dart';
import 'package:yp/router.dart';
import 'package:yp/widgets/bottom_sheet.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/widgets/dialog.dart';
import 'package:yp/widgets/index.dart';
import 'package:yp/widgets/shop.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 分享有礼

class ShareGift extends StatefulWidget {
  const ShareGift({Key key}) : super(key: key);

  @override
  _ShareGiftState createState() => _ShareGiftState();
}

class _ShareGiftState extends State<ShareGift>
    with SingleTickerProviderStateMixin {
  // AnimationController _controller;
  RefreshController _refreshController =
      RefreshController(initialRefresh: true);

  List<String> _infoList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];

  @override
  void initState() {
    super.initState();
    // Future.delayed(Duration(milliseconds: 100), () {
    //   _dialog();
    // });
  }

  @override
  void dispose() {
    super.dispose();
    _refreshController.dispose();
  }

  void _onLoading() async {
    await Future.delayed(Duration(milliseconds: 1000), () {
      _infoList.addAll(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']);
    }).whenComplete(() {
      if (mounted) {
        _refreshController.loadComplete();
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
        BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width,
            maxHeight: MediaQuery.of(context).size.height),
        designSize: Size(750, 1334),
        orientation: Orientation.portrait);

    return Scaffold(
      appBar: YPNavigationBar('分享有礼'),
      body: SmartRefresher(
        enablePullDown: false,
        enablePullUp: true,
        footer: ClassicFooter(),
        controller: _refreshController,
        onLoading: _onLoading,
        child: CustomScrollView(
          physics: BouncingScrollPhysics(),
          slivers: [
            SliverToBoxAdapter(
              child: Stack(
                children: [
                  Padding(
                    padding: EdgeInsets.only(bottom: 32.w),
                    child: Placeholder(fallbackHeight: 400.w),
                  ),
                  Positioned(
                      top: 10.w,
                      right: 0,
                      child: GestureDetector(
                        onTap: () {
                          Get.toNamed(RouteConfig.activityRule);
                        },
                        child: Container(
                            padding: EdgeInsets.fromLTRB(12.w, 6.w, 12.w, 6.w),
                            margin: EdgeInsets.fromLTRB(12.w, 6.w, 0, 12.w),
                            decoration: BoxDecoration(
                                color: Colors.black.withOpacity(0.7),
                                borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(20.w),
                                    bottomLeft: Radius.circular(20.w))),
                            child: Text('奖励规则',
                                style: TextStyle(
                                    color: EsColors.White,
                                    fontSize: 2 * EsSizes.XXS.sp,
                                    fontWeight: EsFontWeights.Normal))),
                      ))
                ],
              ),
            ),
            // SliverSafeArea(sliver:
            SliverPadding(
              padding: EdgeInsets.only(left: 20.w, right: 20.w),
              sliver: SliverGrid(
                  delegate: SliverChildBuilderDelegate((context, index) {
                    return _goodsItem();
                  }, childCount: _infoList.length),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      childAspectRatio: 0.58,
                      crossAxisCount: 2,
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10)),
            ),
            // ),
          ],
        ),
      ),
    );
  }

  /// 商品item
  Widget _goodsItem() {
    return Container(
      decoration: EsDecoration.BoxShadowWithRadius,
      padding: EdgeInsets.all(20.w),
      child: Column(
        children: [
          CachedImage(
              width: 300.w,
              height: 300.w,
              scale: BoxFit.cover,
              path:
                  'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.suning.cn%2Fuimg%2Fsop%2Fcommodity%2F201407300122534036_x.jpg&refer=http%3A%2F%2Fimage.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633619868&t=95a57a19ba050ad266cb09f85fb93503'),
          SizedBox(height: 10.w),
          Text('纪梵希高定香榭小羊皮唇膏',
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  color: EsColors.TitleSecondary,
                  fontSize: 2 * EsSizes.SM.sp,
                  fontWeight: EsFontWeights.Normal)),
          SizedBox(height: 10.w),
          Row(
            children: [
              Text('￥290.67',
                  style: TextStyle(
                      fontSize: EsSizes.SM * 2.sp,
                      color: EsColors.Primary,
                      fontWeight: EsFontWeights.Medium)),
              SizedBox(width: 8.w),
              Expanded(
                  child: Text('￥298.67',
                      style: TextStyle(
                          fontSize: EsSizes.XXS * 2.sp,
                          decoration: TextDecoration.lineThrough,
                          color: EsColors.ContentSecondary,
                          fontWeight: EsFontWeights.Normal)))
            ],
          ),
          SizedBox(height: 10.w),
          GestureDetector(
            onTap: () {
              print('分享有礼');
              YpBottomSheet.showShareGift(context, (selectIndex, selectString,
                  {info}) {
                print(selectIndex.toString() + selectString);
              });
            },
            child: Container(
              width: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 70.w),
              decoration: BoxDecoration(
                  borderRadius:
                      const BorderRadius.all(const Radius.circular(15.0)),
                  border: Border.all(color: EsColors.Primary)),
              child: Text('分享有礼',
                  style: TextStyle(
                      fontSize: EsSizes.SM * 2.sp,
                      color: EsColors.Primary,
                      fontWeight: EsFontWeights.Medium)),
            ),
          )
        ],
      ),
    );
  }
}
